package ssm.integration.controller;

import ssm.integration.pojo.Schedule;
import ssm.integration.service.ScheduleService;
import ssm.integration.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * description: 控制层
 *
 * 1. @RestController = @ResponseBody + @Controller 返回JSON格式
 * 2. @RequestMapping("") 类公共访问路径
 * 3. @GetMapping("") 根据需求描述，指定访问路径和请求方式。路径参数接值 `/{}`，实现分页查询。
 * 4. @PathVariable 接收路径参数
 * 5. @Autowired 从IOC容器调用Service层，要用接口接值，用实现类无法接值
 * 6. 调用Service层接口方法，返回结果模板 R
 * 7. @Slf4j 使用日志接口，logback.xml 是日志实现，会根据xml配置的日志格式进行输出。日志 {} 为占位符。
 * 8. @Validated 启动参数校验
 * 9. @RequestBody 接收消息体的JSON格式数据
 * 10. @PutMapping 指定请求方式，做修改操作
 * 11. @CrossOrigin 防止浏览器做同源策略的访问限制，允许前后端联调时访问后端
 */

@CrossOrigin
@RestController
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize,
                  @PathVariable int currentPage){

        R r = scheduleService.page(pageSize,currentPage);
        //sl4fj
        log.info("查询的数据为：{}", r);
        return r;
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id){
        R r = scheduleService.remove(id);
        return r;
    }

    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result){
        if (result.hasErrors()){
            return R.fail("参数为null,不能保存！");
        }
        R r = scheduleService.save(schedule);
        return r;
    }

    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule, BindingResult result){
        if (result.hasErrors()){
            return R.fail("参数为null,不能修改！");
        }
        R r = scheduleService.update(schedule);
        return r;
    }
}
